import {Component, Input, OnInit} from '@angular/core';
import {RoleResourse} from "../../../../model/RoleResourse";
import {NzTreeVo} from "../../../../vo/NzTreeVo";
import {ManageService} from "../../../../services/manage.service";
import {AlertService} from "../../../../services/alert.service";
import {RoleResourseParm} from "../../../../model/RoleResourseParm";

@Component({
  selector: 'app-gwcdsqedit',
  templateUrl: './gwcdsqedit.component.html',
  styleUrls: ['./gwcdsqedit.component.css']
})
export class GwcdsqeditComponent implements OnInit {

  @Input()id:string
  cd:Array<NzTreeVo>
  yxcd:Array<string>
  result:Array<RoleResourse>

  //
  constructor(private manageService:ManageService,
              private alertService:AlertService) {
    this.cd = new Array<NzTreeVo>()
    this.yxcd = new Array<string>()
    this.result = new Array<RoleResourse>()

  }

  ngOnInit(): void {
    this.getcdArrayNztree()

  }

  getcdArrayNztree(){
    this.manageService.getRightNzTreeByType("MENU").then((data:any)=>{
      if(data.msg == "ok"){
        this.cd = data.list
        this.getyxcdArray();
      }else{
        this.alertService.showError("根据类型生产菜单树失败")
      }
    })
  }

  getyxcdArray(){
    this.manageService.getResourserTreeByTypeAndRole(this.id,"MENU").then((data:any)=>{
      if(data.msg == "ok"){
        this.yxcd = data.list


      }else{
        this.alertService.showError("根据类型生产菜单树失败")
      }
    })
  }



  save(){
    let tosavearray=new Array();
    for(let i=0;i<this.yxcd.length;i++){
      let r=new RoleResourse();
      r.role=this.id;
      r.resource=this.yxcd[i]
      r.type="MENU";

      tosavearray.push(r);
    }
    let roleresouceparm=new RoleResourseParm();
    roleresouceparm.role=this.id;
    roleresouceparm.list=tosavearray;
    roleresouceparm.type="MENU";

    this.manageService.saveRoleResource(roleresouceparm).then((data:any)=>{
      if(data.msg == "ok"){
        this.alertService.showSuccess("设置成功")
        this.getcdArrayNztree()
      }else{
        this.alertService.showError("设置失败")
      }
    })


  }

}
